package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.constant.RedisConstant;
import com.atguigu.gmall.model.product.BaseCategory1;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.rpc.product.BaseCategory;
import com.atguigu.gmall.product.mapper.BaseCategory1Mapper;
import com.atguigu.gmall.product.service.BaseCategory1Service;
import com.atguigu.gmall.cache.annotation.GmallCache;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;


@Service
public class BaseCategory1ServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategory1Service {

    @Resource
    BaseCategory1Mapper baseCategory1Mapper;

//    ConcurrentHashMap<String, List<BaseCategory>> categoryCache = new ConcurrentHashMap<>();


    @GmallCache(cacheKey = RedisConstant.CACHE_CATEGORYS)
    @Override
    public List<BaseCategory> getAllCategoryForIndexPage() {

        List<BaseCategory> page = baseCategory1Mapper.getAllCategoryForIndexPage();
        return page;
        //缓存的使用逻辑
        //1、先看缓存是否有
//        List<BaseCategory> cacheData = categoryCache.get("getAllCategoryForIndexPage");
//        if (cacheData == null) {
//            //缓存没菜单数据，查数据库
//
//            categoryCache.put("getAllCategoryForIndexPage", page);
//            return page;
//        } else {
//            return cacheData;
//        }
    }


    //    @GmallCache(cacheKey= RedisConstant.SKU_PATH+"#{#args[0]}")
    @Override
    public BaseCategoryView getCategoryPathBySkuId(Long skuId) {

        return baseCategory1Mapper.getCategoryPathBySkuId(skuId);
    }
}
